package com.example.farmer.Fragment.Buyer_Home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.farmer.Adapter.Product.Product_Adapter;
import com.example.farmer.Database.ProductPublishDatabase;
import com.example.farmer.Entity.ProductPost;
import com.example.farmer.R;

import java.util.ArrayList;
import java.util.List;


public class VegetableFragment extends Fragment {
    //    private ClassAdapter classAdapter;
    private ArrayList<String> vegetableList;
    private RecyclerView postrecyclerView;
    private TextView notNews;
    private Product_Adapter adapter;
    private ProductPublishDatabase productPublishDatabase;
    private List<ProductPost> productPostList;
    private final MutableLiveData<List<ProductPost>> seedLiveData= new MutableLiveData<>();

    public static VegetableFragment newInstance(List<String> vegetableList) {
        VegetableFragment fragment = new VegetableFragment();
        Bundle args = new Bundle();
        args.putStringArrayList("vegetableList", (ArrayList<String>) vegetableList);
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            vegetableList = getArguments().getStringArrayList("vegetableList");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.class_fragment_fruit, container, false);
        notNews = view.findViewById(R.id.not_news);

        // 初始化数据库并获取所有帖子
        productPublishDatabase = ProductPublishDatabase.getInstance(getContext());
        productPostList = productPublishDatabase.getPostsByProductType("蔬菜");

        // 这里可以设置Adapter来显示列表数据
        postrecyclerView = view.findViewById(R.id.recyclerView_post);
        postrecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new Product_Adapter(productPostList, getContext());
        postrecyclerView.setAdapter(adapter);

        seedLiveData.observe(getViewLifecycleOwner(), this::updateUiWithPosts);
        refreshNewsData();
        return view;
    }
    @Override
    public void onResume() {
        super.onResume();
        // 当Fragment可见时刷新数据
        refreshNewsData();
    }
    private void refreshNewsData() {
        // 从数据库获取最新数据
        productPostList = productPublishDatabase.getPostsByProductType("蔬菜");
        // 更新LiveData的值
        seedLiveData.setValue(new ArrayList<>(productPostList));
    }
    private void updateUiWithPosts(List<ProductPost> productPostList) {
        if (productPostList != null && !productPostList.isEmpty()) {
            adapter.updatePosts(productPostList);
            notNews.setVisibility(View.GONE); // 假设没有数据时显示notNews
        } else {
            notNews.setText("没有最新内容哦");
            notNews.setVisibility(View.VISIBLE);
        }
    }
}